Should probably be extracted and unit tested