fixed code for the case of calling register many times