improoved terminal read function