refactor the code a bit