speed improvements, clean up and restructure