cleaned up code and fixed a bug in application_controller.