code cleanup and fix build problems