Remove some redundant elements and add if-else condition