Changed code structure for testability

StringBuffer is also deprecated