Refactor and repair. It now all seems to be working...