Fixed faulty data counting function