some code refactored in reference to: https://github.com/globaleaks/GlobaLeaks/issues/80