Issue #19, #27: Massive refactoring.