Added a bit of error checking....needs to be tested