Reorganize code, clean up