Completed demo code
Optimizations and solved compile error