Refactor for better DI, include tests