Reorganize code a bit