Refactor, tests and pep8 for the gallery module.