Migration of the code from script to library
The code still crashes, but debugging will be much easier. Also much faster!