previousDepth does need to be outside the function. doh@me.