The memory management could be optimized. it is not. This is a fairly naive implementation.