refactor to accept input from stdin