some codes refactored
added comments to database.php