Issue #11. Further refactorings and fixes.