Probably the easiest stuff to break out are the UI functions from the end of the file. Let me have a think about how to split it up.