improve the calcDepth function