messy code but almost working depth search