code cleanup and fixes