Fixes and optimizations