refactor a bit to make the code clearer