Merged changes from the unreadable branch into this one and fixed some other stuff.