Incorporate escherdragon's fixes to stop segfaulting