Refactor code according to review comments