Reorganize code

Still trying to find a good design for this.