Various housekeeping and bug fixes