improve get ports function