Fixed tests and more optimizations