Declare missing global vars and fix gateway deletion