Improve the way that hostname is obtained, should be safer in CLI scripts, fixes #159