Fix a lot of issues, and some cleanup