added hooks, and few minor refactoring