refactored and made register on demand