Fixed a number of minor bugs