Revert "Revert "Fixed a couple of bugs. It's working now.""

This reverts commit acfd2dfd133444d53c20035a277a8401ffcae09e.