edit:rewritten in python