Some Refractoring, and removal of test code