refactored count_links