Add two tests, fix bunch of bugs in the process.