Much faster (althrough probably not optimal) and generic solution.