fixed all the code, including a bug in aggregate