Minor optimizations and code cleanup