refactor out code to be simpler