Got algorithm to work with levels and recursion