Faster project loading in classpath computation

Cleans up and supersedes #840.
Adds cycle detection.