fix defects and cleanup