some cleaning up of code, and moving things into reset function.