fix a number of bugs, uninitialised vars and calls to the wrong object