added optimizations and more