more code cleanup and bug fixes