Added Exceptions and refactored some code