Misc. fixes and add tests