Refactor with headers and fix tests