Add more scoping as well as exception handling and threading