Pseudocode added.