Error handling

Cleaner way of handling errors, I think. Really only look at the last commit. The others are there because a previous pull request has not been merged yet.