Various fixes and tests