reimplement count function