Changed to a more obvious empty check (could need changing).
Moved second isEmpty() invocation to correct place.