more optimizations and cleanup