refactor working, but not pretty