More fixes and optimizations