Test using reference files

Also some fixes that were required to generate the reference files.