Fixed #1117 and fixed references to variables that doesn't exist.