radical refactoring


sorry for hard to read tabs/spaces diffs