fixing the old algorithm