algorithm and everything working