Added some tests + minor refactoring