Cleaned and extracted duplicating code into GenerateObjectsToScript