Refactor internals a little
add batch of tests