optimizing "commit" function