Several more fixes and tests