improve this code dramatically;

# User registration route
@app.route('/register', methods=['GET', 'POST'])
def register():
    form = RegisterForm()  # Create registration form instance
    if form.validate_on_submit():  # Validate the submitted form
        if len(form.username.data) < 3 or len(form.username.data) > 150:  # Username length check
            flash('Username must be between 3 and 150 characters.', 'danger')
            return redirect(url_for('register'))  # Redirect back to register
        if User.query.filter_by(username=form.username.data).first():  # Check for existing username
            flash('Username already exists.', 'danger')  # Error message
            return redirect(url_for('register'))  # Redirect back to register
        hashed_password = generate_password_hash(form.password.data)  # Hash the password
        new_user = User(username=form.username.data, password=hashed_password)  # Create new user
        db.session.add(new_user)  # Add user to session
        db.session.commit()  # Commit to the database
        flash('Registration successful! You can now log in.', 'success')  # Success message
        return redirect(url_for('login'))  # Redirect to login
    return render_template_string('''
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <title>Register</title>
        <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
    </head>
    <body>
        <div class="container mt-5">
            <h2>Register</h2>
            <form method="POST">
                {{ form.hidden_tag() }}  <!-- CSRF token -->
                <div class="form-group">
                    {{ form.username.label(class="form-label") }}  <!-- Username label -->
                    {{ form.username(class="form-control", placeholder="Username") }}  <!-- Username input -->
                </div>
                <div class="form-group">
                    {{ form.password.label(class="form-label") }}  <!-- Password label -->
                    {{ form.password(class="form-control", placeholder="Password") }}  <!-- Password input -->
                </div>
                {{ form.submit(class="btn btn-primary") }}  <!-- Submit button -->
            </form>
        </div>
    </body>
    </html>
    ''', form=form)  # Render registration page

# User login route for authentication
@app.route('/login', methods=['GET', 'POST'])
@limiter.limit("5 per minute")  # Rate limit for login attempts
def login():
    form = LoginForm()  # Create login form instance
    if form.validate_on_submit():  # Validate the submitted form
        user = User.query.filter_by(username=form.username.data).first()  # Fetch user from database
        if user and check_password_hash(user.password, form.password.data):  # Check credentials
            session.clear()  # Clear existing session to prevent fixation attacks
            session['username'] = user.username  # Store username in session
            flash('Logged in successfully.', 'success')  # Success message
            return redirect(url_for('home'))  # Redirect to home
        flash('Invalid credentials. Please try again.', 'danger')  # Error message
    return render_template_string('''
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <title>Login</title>
        <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
    </head>
    <body>
        <div class="container mt-5">
            <h2>Login</h2>
            <form method="POST">
                {{ form.hidden_tag() }}  <!-- CSRF token -->
                <div class="form-group">
                    {{ form.username.label(class="form-label") }}  <!-- Username label -->
                    {{ form.username(class="form-control", placeholder="Username") }}  <!-- Username input -->
                </div>
                <div class="form-group">
                    {{ form.password.label(class="form-label") }}  <!-- Password label -->
                    {{ form.password(class="form-control", placeholder="Password") }}  <!-- Password input -->
                </div>
                {{ form.submit(class="btn btn-primary") }}  <!-- Submit button -->
            </form>
        </div>
    </body>
    </html>
    ''', form=form)  # Render login page