CodeClimate

This change refactors some code to remove duplication.