fixes and optimizations