added a lot of error checking so the client won't blow up on a bad input