Modified to fix bugs