Modified code according to review comments