refactor into many more smaller functions