optimized memory allocation and add option for adjusting granularity