Track array lengths in optimizer, fix monad bugs
Add documentation