Fixed a large number of minor bugs