psuedo code for doug's code