Added Quit function, reorganized parts of the code.