In general, the above big nested `if-else` makes the logic a bit unclear to trace. Please restructure it.