converted pseudo logic to torque script