A little refactoring and addition of tests