Finishing touches; rearranging code into structured functions