Rewrite pow to avoid weird double free bug.