refactor: extract utilities, add tests