refactor lots of code