fixed up the pseudocode to be nicer