finished core algorithm