Basic working algorithm