Some cleaning and bug fixes