Move logic for adding sidebars into separate function and add tests.