Done a load of tidying and error handler work

Error handling work and tidying up... it was a messy night, just don't
ask.