Re #945 Refactoring some functions.