refactor into multiple small functions