improved the getstr function to avoid buffer overflow