Refactor repeated code and remove elseif's