BUG-1024: reorganize code for clarity