Updated code to resolved duplicate key problem