Removed extraneous code and fixed some logic.