improve code, add 'autoEnd' method.