optimizations and code clean up