performed further optimizations. needs testing