Advanced logic for irl. Refactored code.