Small optimizations and code cleanup