More refactoring and attempt at fixing #656.