refactor some code
fix recursive call of register -> back to previus version with two functions