Revert "fix syntax error introduce in aeb0b5dbb7151c413f2af14fb23df3dca53d6712"

This reverts commit 0ad23583b75cf9705fba0e4cb50a7047cdb9c285.
Revert "Made code neater by condensing"

Second else can't be reduced to elif

This reverts commit aeb0b5dbb7151c413f2af14fb23df3dca53d6712.
reimplement correct change introduced by reverted commit aeb0b5dbb7151c413f2af14fb23df3dca53d6712