working version; commented