Refactor some js to make it clearer

Extract the function that builds service elements .