Improve resync handling code.
- Force resync count to be an integer
- Tidy up a very confusing set of if tests (correct, but unreadable)