Some refactorings and trace logging outputs in Gateway and Portal (belongs to #242)