refactoring code and adding exception class