Minor fixes for bugs introduced in last commit and add tests for it.