refactored code little bit to make it more readable.