Major rearrangement of preamble

New packages, better code structure, new macros, etc.