move var d outside function