Cleaned up the code in preparation for plugging together all the pieces.