some cleanup and optimizations