Refactored a bit and wrote the some of the missing tests. more to come....