Fix up and add tests