Remove duplicates and fixed a bug