More planning

A little refactoring, and also added the inner loop that searches for expansion and joins