Some optimizations and cleanup