bug fixes, optimizations