AMD support, rev 2

Based on #526, but done the d3 way, with no global pollution.