Updated with some additions to the algorithm and conclusion.