Extracted some logic