Complete rewrite, variable parsing is now handled better and I put everything into a class