a big refactor