refactored error handling to be much clearer on what is a bug and what is a handled error