Implemented cycle checking on DFS. still horrible though.