Code rearrangement in common.php to fix #1223