massive refactor part 1 (to be continued)