refactoring to use StringBuilder instead of StringBuffer.
refactor and add testcase.