added automatic detection of depth, still some troubles