Refactor to use ORM-like entity/repository structure, fix some old errors, add Stats framework