finish first stage of rewrite (move doubled code from views and ajax to engine)