Improved and removed bugg in the basic algorithm