Refactor code, add tests and CI