Fix and organize variable references