big refactor. First document test passes