added code for doing all of the second pass in one go