change coding style to simplify the control flow