Applied patch with better logic