pretty decent refactor