Added null pointer handling