Move factorization code and long functions to specialized classes in algorithms package