fix spec problem and handle memory leak