remove dead code
added missing vars
fixed misc bugs