Use C99 and fix handling of failed reads