Final polishing, added typecast on magnitude calculation to avoid truncation.