some fixes and optimizations