Refactor find_library code