Optimization

This commit has been in the works for many days, and I don't remember every change, but basically it's optimization