Fix bugs in group formation and enhance performance by caching already calculated formations data for future use

Signed-off-by: MHesham <mhesham.fcis@gmail.com>