refactor to make it work right