Fix hooks calling. Refactor code