Code rearrangement

Functions split into groups
All functions ordered alphabetically within groups.