Made all algorithm work