refactor slightly to run at the end