fix various bugs and misunderstanding of the requirement