Added some free()s to prevent memory corruption.