made some fixes and optimizations