fixed bugs, added debug couts (commented out at the moment)