Main algorithm done