A bunch of bug and coding fixes