Optimizations

Code cleanup, refactoring, etc.  Still needs a bit more work.