updated save function to stop recursion