add null pointer check, make code more robust