Extracted logic to function