Numerous fixes and additional tests