Fixes several undefined variables and indexes