Refactoring principal too long function