refactoring code a bit