Fixed bug and added debug messages