Utility code from Madefire