new algorithm, all outputs are right, no excessive plinks; done manually, need to code it (and add plinks)