added a wee bit of error checking, still needs more work