I think this implementation of the P(x) function is broken, but it's a start.