Added python3 compatibility using reduce from functools