made some optimisations (depth first is slightly broken at the moment).
a touch more optimisation and fixed the depth first search.