Bug fixes, added debug messages