Reorganize and modernize code