refactoring and exception handling