Simplified code greatly and made it fit in toolbox