major code refactor to support unit tests

Also:
 * 100 new unit tests in testbtcr.py
 * test wallet files included in test-wallets/
 * additional performance improvements: faster sorting with AnchoredTokens, remove speed penalties for some features if not used in a particular run, more local aliases for globals, avoid len where possible