optimizations, added config options, cleanup