Added some tests and did a little refactoring