Refactoring the code a bit