General cleanup and optimizations