numerous bug fixes and optimizations