Minor optimizations and bug fixes