Reorganize code for better flexibility