Refactor some more, add tests for /stats routes