Added rebasing. Replaced some sections with list comprehensions.