Changed the error handling and made som syntactic choices to make it more
readable