Updated with all working code