refactor, but not quite working