added safety checks to retain functions