finished refactor to make it run