Re-factor calcDepth function