Moved code around to read smoother.