complete refactor, now working