Recursive solution found. Needs testing