Fixed possible NPE in certain circumstances