refactor code structure, simplify if-else judgement