Refactoring to make code more self-documenting.

Extract methods, change visibility scope of non-hook callback methods etc.