Started implementing the loop optimiser.