simplify things a bit and minor speed optimizations