working three pass algorithm with correct depth output