refactor into modules and add a couple tests