final fixes. This code is really kinda awful and I'm not proud of it