Make more functional

Use reduce instead of loops
Merge pull request #2 from danjo133/patch-2

Make more functional