Slightly better exception handling and some refactoring #90