updated to use concurrent memory allocation functions, and made it more efficient