Added potential compress and cleaned up the code