added cycle function and code improved

added cycle function and code improved