Move error-checking code into methods and improve error messages so you can find what the hell went wrong