refine naive solution