Adding debug output and fixing loop break