refactored code and added some complexity