Added some conditions to remove unnecessary duplicate operations