now only traverses document once