Better exceptions and some refactoring.