map exceptions correctly and refactor a few things.