Moved Datastore flush and added null check.