added more validations and error checking