Added bug fixes and debug code

Will need to rollback debug code and/or ifdef out once everything works