Move dependency checking into separate function.
Use correct way to remove stuff from a set.