p23 in python, a little slow though, should optimize