Moved common code into functions.
Fixed alignment.