Arrange code, add median tests