refactor probably close. still needs tests