Now in a working and hopefully much more readable state (as in easier to follow).