Code climate refactoring

Refactoring to reduce duplication and complexity.
Space inside { missing.<br>Space inside } missing.