Ex. 24.8 without optimization
Optimizations added, but it's nasty
Cleaned up optimization