MUCH better now. Far more readable with a bit of modularization. Probably could do more.