optimizations and more unit tests