Big refactoring and adding support for closing the browser on process exit.