Refactored Code

refactored code and incorporated abi's suggestions