improved and refactored registration code