Trying to unspaghettize code