refactored to use stdin/stdout and added test