Fixing logic to prevent unwanted appending of String