the triple nested for loop is ugly. try to find a better solution.
the triple nested for loop is ugly. try to find a better solution.