Completed calcNumTouching function