Some refactoring to use exceptions