refactored to support multiple cases from stdin