Fixed possible null pointer exceptions