Fix an issue with more than 64k deps, spotted by maximuska.

Also add a test for this case, which would have spotted the issue too.