Optimises algorithm a bit.