code for exercise with bug fixes