Lots of Unit tests + some refactoring

You should watch my repo, merging your changes was a major PITA.