improving sentence generation algorithm