PCT-637: Added tests, reorganized code