Refactor index.php and a few fixes.