Moved a lot of code into separate methods.