skeleton algorithm it is almost working