Added quit, some refactoring