Refactoring and fixing HAL.