Implementing a count function