Moved gateway shutdown to time when app is exiting.
Added documentation.