Clean up

reorganized much of the code