prepare common algorithm function