fix the logic error of fragment and make the code easier to read