added throw exception and refactored