Added minor things: debug checks. Better comments