Modified to remove bugs