This function can time out, the core bit lines 49-60 should be in a for loop that iterates 5-10 times before exiting.