Refactor code for fetching datasources and for layout grabbing