Start to clean up the code. previousDepth has no need to be a file-level variable.