updated pseudo-code