Added cycle detection algo. Minor changes to Connection.java as well.