Changed to use properly matrices and depth