script optimization, functions, affinity