Fix calc_ip function