fix nil limit bug and create helper function