adding error checking to helper function