Code Refactor. Fix broken test cases.

Signed-off-by: Pengcheng Bian <pbian@actuate.com>