add some exceptions and refactoring