Python implementation of algorithm found in