refactor *almost* working