Fix errors and add tests