Finally a functionally correct full implmentation

Validated as yet only for 1.in.
Buy I assume it to be more or less correct.