Cleanup

A lot of cleanup and generalization