Modify substring related part to fix bug