Memory cleanup.

Added functions to free states from memory, and removed reallocating the output array.