rewrite port calculation