refactoring a bit of code