some cleanups and shuffling code around