Simplified and cleaned up code a bit. Refactored variables and fix minor bug when restarting.