OPT: speed up the code to make it working on data of large dimensions (at least works well on data of dimensions 120 x 120 x 600)