If you move these codes into a hash, it will simplify the logic.