Fixed a few bugs

Thanks to Matthew Dunn for helping me identify the errors!