Fixed various errors with undefined variables and missing imports.