Cleanup, part I
Did a lot of tweaking and debugging