generalized function to be used in for or parallel loops