updated to handle 0
updated to count before index
added solution from page for ref