added cursor preservation. Crude but it works