First attempt at the algorithm.

Need to refactor the initialising code. Specifically, the row linking code
is terribly inefficient.