Refactoring code slightly