Fix trajectory filename increment and file close/open