Fixed a bug in aggregate, added tests to cover case