refactored almost all cases