factorize the get messages part