added dynamic legend and fixed count bug