some cleanup and bug fixes