Tests and a little refactoring