Added more validation and error checking.