adjusted to accept stdin