Added 3D support (z dimension)