First pass of pulling out code, partially for babckup, partially for sharing :)