Refactored with help from CodeClimate