Fixes for a couple of bugs, various other minor refactorings and debug features, more work on mining