Fixed matrix errors and added generalized link setup function