added a bunch of optimizations