Added quit function

Also minor code cleaning