revision and cleanup. added pseudocode.