Refactor and add some quick tests