code refactor for better understanding