added functional (but damn slow) python implementation