Massive rewrite to Python ;)