Cleaning and bug fixes