Add smarty mode and fix several small bugs