Moved code around for a little more structure.