Fork from Justin Smith's script