Refactoring code a bit