refactor to get rid of code complexity