Backported important changes from Kura's Gastly into Hauntr.