Brian's bugfixes and general cleanup