Some refactoring and improved join completion behaviour (with tests).
Minor bugfixes.