code cleanup and minor fixes.