try to make this spaghetti code more readable.