Added check for 0 elements to prevent unnecessary queries