revised according to code review