added nodes and improved pseudo-code after talking to Shan