Shift some stuff around and modularize the code.