Second commit -

Added error and refactored the code