refactor some pieces of code