Program is in working state using the example input. Pretty ugly though. Needs some cleanup.