Code cleanup and implements thread delete
Fixing null pointer exc