Move the element formating code into a separated function