added hooks and cleaned up code