IF and ELSE statements being executed

Should I put some small delay to fix that?

No. Do it properly.