Added comments and improved threading