Fixed possible null pointer bugs causing crashes