Fixed memory issue, changed the way to stop the thread, cleaned-up the code.