Further optimizations and bug fixes