refactor the code to fragment