Various clean up and optimizations