Rearranging code

Put functions where I feel they belong
More code rearrangements