Refactored sqrt function