updated with error checking