updated new functioning code