cleaned code
moved reduce to core function
reduce maintains context
Merge branch 'reduce'