Judge rules Montana legislator can remain in office