Fix rank issues

Credit goes to paulicit!