Updated to include some more error checking