Used pow instead of manual exponentiation, huge speed improvements