refactored HMR code further