Reorganize code, cleanup