Performance improvements, memory leak fixed