Minor bug fixes and optimizations