refactor solve code