refactoring code & fix bug with repeated registration