almost done with algo 3