Fixed a few bugs, removed some unreachable code