Re #996 Some preliminary code cleaning.