Added some validation and error checking.