Use functools for reduce for python 3 compatibility