added lots of error checking