Added debug param and cleaned up code