I would suggest looking at other sources of "guidance".
That program is excessively complicated and appears to be doing things that are completely unnecessary, for example, you should not detach, then reattach interrupts.
I would look for simpler code, such as found here.
Better yet, study the data sheet and the library code, so that you understand what every line of that program is doing (or not), and remove all the junk.