Fixed logical error and cleaned up some unnecessary code