Most of the final code.