Refactored task to make code a bit more understandable