use reduce method instead of cycle