Refactored into more readable code