In several places in the code check isEmpty instead of checking count.