code improvements

mostly performance improvements and reducing amount of CPU pressure