Implementation of reduce :)