further optimizations; pass all tests