Minor code cleanup and added duplicate check.