using StringBuffer instaed of String, exit logic changed