optimizations and cleanup