Some refactor, add tests