Final version with commented code and depth limited search