Updated angle calculation code