Code climate refactoring

Refactoring to reduce duplication and complexity.
Indent `when` as deep as `case`.