refactored code.... a lot