improved countchar method