better algorithm; one pass over each file and only store ids