inserted more if else branches for better semantics