First optimizations. Needs testing!