some code cleanup and optimizations