Fix #240 and do some random cleanups.