Changed reduce calls from one-based to zero-based
Added more powerful tests and fixed code for mean