more fixes and optimizations