Most of the logic written.