updated algorithm to not rely on \b and to use a temporary buffer instead