}
        } catch (Exception $e) {
            $this->flash->addMessage('fail', $e->getMessage());
            return $response->withRedirect($this->router->pathFor('person_edit', $person->id));
        }
        // Success, redirect to the building
        return $response->withRedirect($this->router->pathFor('building', ['id' => $person->building_id]) . "?pid={$person->id}");
    })->setName('person');
    $this->get('/edit', function ($request, $response, $args) {
        $item = R::load('person', $args['id']);
        return $this->view->render($response, 'person_edit.twig', ['person' => $item, 'messages' => $this->flash->getMessages()]);
    })->setName('person_edit');
});
$app->get('/people', function ($request, $response) {
    $items = R::findAll('person');
    usort($items, cmpByKey('address'));
    return $this->view->render($response, 'people.twig', ['people' => $items, 'messages' => $this->flash->getMessages()]);
})->setName('people');
$app->post('/people', function ($request, $response) {
    try {
        $new_person = $request->getParsedBody()['person'];
        if (empty($new_person['building_id']) || $new_person['building_id'] == 0) {
            throw new Exception("Person must be added to a valid building");
        }
        $person = R::dispense('person');
        $differences = array_diff_assoc($new_person, $person->export());
        if (empty($differences)) {
            throw new Exception("Nothing changed");
        }
        foreach ($differences as $key => $value) {
            $person[$key] = $value;
             $this->flash->addMessage('fail', 'Please enter an address');
             return $response->withRedirect($request->getUri());
         }
         // Query 411.ca to get people
         $new_people = getPeopleByAddress($formdata['address']);
         if (count($new_people) < 1) {
             $this->flash->addMessage('fail', 'Sorry, no people could be retrieved at: ' . $formdata['address']);
             return $response->withRedirect($request->getUri());
         }
         $people = $building->ownPersonList;
         usort($new_people, cmpByKey('phone'));
         return $this->view->render($response, 'building.twig', ['new_people' => $new_people, 'building' => $building, 'messages' => $this->flash->getMessages(), 'people' => $people]);
     } elseif ($formdata['form_add_people']) {
         // Add people to building
         $new_people = $formdata['people'];
         usort($new_people, cmpByKey('unit'));
         foreach ($new_people as $p) {
             $person = createEntityWithArray('person', $p);
             $building->xownPersonList[] = $person;
         }
         // Save building
         R::store($building);
         $this->flash->addMessage('success', 'Added ' . count($formdata['people']) . ' new people to building.');
     }
 } catch (Exception $e) {
     $this->flash->addMessage($e->getMessage());
     return $response->withRedirect($request->getUri());
 }
 return $response->withRedirect($request->getUri());
 // elseif ( !empty($formdata['form_add_person']) ) {
 // 	// echo 'NOT YET';