Refactor + improve tests