CodeClimate improvements

This change reduces some duplicated code to make CodeClimate happier.