Copied psuedocode from paper.