Bug fixes for duplicate key errors