Refactor and add mock tests