updated pseudo code, please take a look