Tests and small refactors