Pure Python reference implementation