Fixed a number of small bugs