used isEmpty instead of length == 0