added more error handling and custom exceptions.