Refactor code different cases clear, fix #1876