refactor port placement logic