Reorganize code to be more readable