Refactor application termination