fixed several bugs, mostly undeclared global vars