Refactoring to make code more self-documenting.

Extract methods, change visibility scope of non-hook callback methods etc.

See line comments on PR for more info.