Neater code

Unfinished. No way to test.