finds best solution