refactored code as per review comments