Using pow_hash here doesn't make any sense. You want to use "target" instead, and then you should be using p2pool_data.target_to_difficulty on it. Also, the comparisons below should be <= instead of <.