Minor algorithm improvement