  )] );
  Exporter::export_ok_tags('all');
 
-@@ -257,10 +258,62 @@ sub NLSync {
+@@ -237,33 +238,125 @@ sub NLSync {
+
+     my $cardnumber = SOAP::Data->name( 'lnr' => $patron->cardnumber );
+
+-    # Call the appropriate method based on syncstatus
++    # We will now perform the SOAP call. After that we must carefully
++    # inspect the response and take actions depending
++    # on its contents. There are several situations we have to handle.
++    # NL is very picky about what it accepts, and issues might stem
++    # from missing or bad data or inconsistency with existing data in NL.
++    #
++    # We might need to perform multiple extra SOAP calls to fix the
++    # issue, so in order to not risk enter into an infinite loop, we
++    # limit the number of times we try to fix things before we give
++    # up and return the last response.
++
++    my $max_tries = 3;
++    my $ok;
+     my $response;
+-    if ( $sync->syncstatus eq 'edited' || $sync->syncstatus eq 'new' ) {
+-        my $soap_patron = _koha_patron_to_soap( $patron );
+-        if ( $sync->syncstatus eq 'edited' ) {
+-            $response = $client->endre( $cardnumber, $soap_patron );
+-        } elsif ( $sync->syncstatus eq 'new' ) {
+-            $response = $client->nyPost( $soap_patron );
++
++    for (my $i=1; $i<=$max_tries; $i++) {
++        # Call the appropriate method based on syncstatus
++        if ( $sync->syncstatus eq 'edited' || $sync->syncstatus eq 'new' ) {
++            my $soap_patron = _koha_patron_to_soap( $patron );
++            if ( $sync->syncstatus eq 'edited' ) {
++                $response = $client->endre( $cardnumber, $soap_patron );
++            } elsif ( $sync->syncstatus eq 'new' ) {
++                $response = $client->nyPost( $soap_patron );
++            }
++        } elsif ( $sync->syncstatus eq 'delete' ) {
++            $response = $client->slett( $cardnumber );
+         }
++
++        ($response, $ok) = _responseOK($sync, $response, $patron, $client);
++        last if $ok;
+     }
+-    if ( $sync->syncstatus eq 'delete' ) {
+-        $response = $client->slett( $cardnumber );
+-    }
+
+-    # Update the sync data according to the results
+-    if ( $response->{'status'} && $response->{'status'} == 1 ) {
++    return $response;
++}
++
++sub _responseOK {
++    my ( $sync, $response, $patron, $client ) = @_;
++
++    # Handle successfull operation
++    if ( $response->result->{'status'} && $response->result->{'status'} == 1 ) {
+         if ( $sync->syncstatus eq 'delete' ) {
              # Turn off any further syncing
              $sync->update( { 'sync' => 0 } );
          }

             SOAP::Data->name( 'p_postnr'    => $patron->zipcode     )->type( 'string' ),
             SOAP::Data->name( 'p_sted'      => $patron->city        )->type( 'string' ),
-            SOAP::Data->name( 'p_land'      => $patron->country     )->type( 'string' ),
+            SOAP::Data->name( 'p_land'      => 'no'                 )->type( 'string'),
             SOAP::Data->name( 'm_adresse1'  => $patron->b_address   )->type( 'string' ),
             SOAP::Data->name( 'm_adresse2'  => $patron->b_address2  )->type( 'string' ),
             SOAP::Data->name( 'm_postnr'    => $patron->b_zipcode   )->type( 'string' ),
diff --git a/members/nl-search.pl b/members/nl-search.pl
index 29ec13e..0fa3ac2 100755
--- a/members/nl-search.pl


* Fix re-sync of imported patron from NL

* Retry fixing bad data with up to 3 calls to NL

* Make sure 'server_tid' is stored in borrower_sync.lastsync after successfull sync.


diff --git a/Koha/NorwegianPatronDB.pm b/Koha/NorwegianPatronDB.pm
index 237ddd7..731bd78 100644
--- a/Koha/NorwegianPatronDB.pm
+++ b/Koha/NorwegianPatronDB.pm
@@ -74,6 +74,7 @@ our %EXPORT_TAGS = ( all => [qw(
