Refactor and  work on #8 to add more back end test cases.
Code reformat according to style.