Add more checks for NULL pointers