Tests and various small fixes