Code cleanum and some refactoring to put common functions in their own module.