fixed memory leak. preallocate vectors for efficiency