Fix possible null dereferences