The structure and most of the algorithm is complete, now to debug...