Create CGS algorithm2 to function