Code cleanup and added an attach state