Fix potential NPE in contains function