pseudocode, refactored solution updated