Fixed a number of bugs