Updated to use a buffer for joining strings