Optimizations

I think this may be what you were asking for. Lots of fiddling around to get it to work and not sure I fully understand it yet but it works. 
@vicentereig