Fix possible null pointer dereferences