Major optimizations

This turned out to be a lot simpler than I thought it would.