Improved pow algorithm