Deal with some IO errors
Cleanup code and formatting

Remove some instances that weren't being used, bring some top level
definitions inside where clauses instead and neaten up the naming scheme
a little bit.