optimization to the logic by merging if conditions