Tests and a few fixes