some good progress, i came up with a (really trivial actually) method to avoid a n^2 loop