Fixed some Bugs

Fixed a few bug pointed out by Michal.