Fix undefined error, and note that this code really needs to be cleaned up