Simplify algorithm and reduce time to 1.54s