Fixed a couple of bugs. Cleaned some code.

Fixed a couple of bugs. Cleaned some code.