Added reduction algorithm. Currently code is messy due to debugging - commiting for sanity before tidying.