proper port number handling for all commands, minor refactoring