working version from professor's code