Refactor and add mathcheck

I see variables ```expanded``` and ```optimized``` are local to ```function property()```, ```packet_idx``` is global. I don't know if this is working OK. Since these variables are local in ```property()``` shouldn't their values be ```nil``` here? Just wondering.