Add some Junit tests and rearrange code