Lots of things

This needs to be refactored and use tests, but it appears to work for now