using O(n ^ 3) algorithm to improve the performance