added checks for NULL pointers