Numerous fixes and cleanup