Optimized integrate function in module poly

Rewrote the integrate function since the old one was quite hacky. The
new one is about 7 times faster in release and 3 times faster in debug