Split auxiliary functions into a module.
First working revision. Code is exhaustively commented.