reorganize code to simplify build