refactor the code a little