refactor all the code