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