conversion to unit-testable design
backtracking implemented, still slow