significant speed optimizations throughout