Code clean up and more tests