Minor bugs fixed, also cleaned up the anti-duplication code