refactoring to make it more functional, still wip