pulled subfunctions out of replace_number and made the second argument logical