optimizations

lots of small changes and code clean up