Redeeming scala modularization

This is a rehash of #597 which supersedes it. The last 2 commits are
mine, the others from @adriaanm.

Review by @skyluc, @adriaanm