Added some error handling and error validation.