added checks for negative cycles and cleaned up code a bit