some code cleanup and fixes