optimizations, bug fixes