Cleanup on console closed & log error.stack

Added log of error.stack and cleanup when the Windows console close.