Updated to do additional error checking