Major refactor.
Added more pseudocode.