Refactored link generation script