code review and some bug fixes