diff --git a/include/class.osticket.php b/include/class.osticket.php
index 84796008eb2b27fe8420c5c33882ddacca0efdd6..d6f1f5533361ce72f6abbe64bd39be47a94bc05b 100644
--- a/include/class.osticket.php
+++ b/include/class.osticket.php
@@ -56,7 +56,8 @@ class osTicket {
         require_once(INCLUDE_DIR.'class.config.php'); //Config helper
         require_once(INCLUDE_DIR.'class.company.php');
 
-        $this->session = osTicketSession::start(SESSION_TTL); // start DB based session
+        if (!defined('DISABLE_SESSION') || !DISABLE_SESSION)
+            $this->session = osTicketSession::start(SESSION_TTL); // start DB based session
 
         $this->config = new OsticketConfig();
 
diff --git a/include/cli/cli.inc.php b/include/cli/cli.inc.php
index cb833c9219859387f485ed805f3694030dfe6ed6..fcecaf4a1aaa71bbb1da16e270329c483e265230 100644
--- a/include/cli/cli.inc.php
+++ b/include/cli/cli.inc.php
@@ -19,6 +19,7 @@
 if(!strcasecmp(basename($_SERVER['SCRIPT_NAME']),basename(__FILE__))) die('kwaheri rafiki!');
 
 define('ROOT_PATH', '/');
+define('DISABLE_SESSION', true);
 define('INC_DIR',dirname(__file__).'/../inc/'); //local include dir!
 
 require_once INCLUDE_DIR . "class.cli.php";
diff --git a/include/cli/modules/file.php b/include/cli/modules/file.php
index 416346939d027b028e404380ff408762e2da06bf..ed7916ad8c7c930ece31fd6b9deb88e7901ad909 100644
--- a/include/cli/modules/file.php
+++ b/include/cli/modules/file.php
@@ -67,7 +67,7 @@ class FileManager extends Module {
         case 'list':
             // List files matching criteria
             // ORM would be nice!
-            $files = FileModel::objects();
+            $files = AttachmentFile::objects();
             $this->_applyCriteria($options, $files);
             foreach ($files as $f) {
                 printf("% 5d %s % 8d %s % 16s %s\n", $f->id, $f->bk,
@@ -79,24 +79,36 @@ class FileManager extends Module {
             break;
 
         case 'dump':
-            $files = FileModel::objects();
+            $files = AttachmentFile::objects();
             $this->_applyCriteria($options, $files);
-            if ($files->count() != 1)
+            try {
+                $f = $files->one();
+            }
+            catch (DoesNotExist $e) {
+                $this->fail('No file matches the given criteria');
+            }
+            catch (ObjectNotUnique $e) {
                 $this->fail('Criteria must select exactly 1 file');
+            }
 
-            if (($f = AttachmentFile::lookup($files[0]->id))
-                    && ($bk = $f->open()))
+            if ($bk = $f->open())
                 $bk->passthru();
             break;
 
         case 'load':
             // Load file content from STDIN
-            $files = FileModel::objects();
+            $files = AttachmentFile::objects();
             $this->_applyCriteria($options, $files);
-            if ($files->count() != 1)
+            try {
+                $f = $files->one();
+            }
+            catch (DoesNotExist $e) {
+                $this->fail('No file matches the given criteria');
+            }
+            catch (ObjectNotUnique $e) {
                 $this->fail('Criteria must select exactly 1 file');
+            }
 
-            $f = AttachmentFile::lookup($files[0]->id);
             try {
                 if ($bk = $f->open())
                     $bk->unlink();
@@ -122,24 +134,34 @@ class FileManager extends Module {
             }
             else {
                 $stream = fopen('php://stdin', 'rb');
-                while ($block = fread($stream, $bk->getBlockSize())) {
-                    if (!$bk->write($block))
+                // reading from the stream will likely return an amount of
+                // data different from the backend requested block size. Loop
+                // until $read_size bytes are recieved.
+                while (true) {
+                    $contents = '';
+                    $read_size = $bk->getBlockSize();
+                    while ($read_size > 0 && ($block = fread($stream, $read_size))) {
+                        $contents .= $block;
+                        $read_size -= strlen($block);
+                    }
+                    if (!$contents)
+                        break;
+                    if (!$bk->write($contents))
                         $this->fail('Unable to send file contents to backend');
                     if (!$type)
-                        $type = $finfo->buffer($block);
+                        $type = $finfo->buffer($contents);
                 }
                 if (!$bk->flush())
                     $this->fail('Unable to commit file contents to backend');
             }
 
             // TODO: Update file metadata
-            $sql = 'UPDATE '.FILE_TABLE.' SET bk='.db_input($bk->getBkChar())
-                .', created=CURRENT_TIMESTAMP'
-                .', type='.db_input($type)
-                .', signature='.db_input($signature)
-                .' WHERE id='.db_input($f->getId());
+            $f->bk = $bk->getBkChar();
+            $f->created = SqlFunction::NOW();
+            $f->type = $type;
+            $f->signature = $signature;
 
-            if (!db_query($sql) || db_affected_rows()!=1)
+            if (!$f->save())
                 $this->fail('Unable to update file metadata');
 
             $this->stdout->write("Successfully saved contents\n");
@@ -152,19 +174,18 @@ class FileManager extends Module {
             if (!FileStorageBackend::isRegistered($options['to']))
                 $this->fail('Target backend is not installed. See `backends` action');
 
-            $files = FileModel::objects();
+            $files = AttachmentFile::objects();
             $this->_applyCriteria($options, $files);
 
             $count = 0;
-            foreach ($files as $m) {
-                $f = AttachmentFile::lookup($m->id);
+            foreach ($files as $f) {
                 if ($f->getBackend() == $options['to'])
                     continue;
                 if ($options['verbose'])
-                    $this->stdout->write('Migrating '.$m->name."\n");
+                    $this->stdout->write('Migrating '.$f->name."\n");
                 try {
                     if (!$f->migrate($options['to']))
-                        $this->stderr->write('Unable to migrate '.$m->name."\n");
+                        $this->stderr->write('Unable to migrate '.$f->name."\n");
                     else
                         $count++;
                 }
@@ -199,7 +220,7 @@ class FileManager extends Module {
          *              is stdout
          */
         case 'export':
-            $files = FileModel::objects();
+            $files = AttachmentFile::objects();
             $this->_applyCriteria($options, $files);
 
             if (!$options['file'] || $options['file'] == '-')
@@ -208,10 +229,9 @@ class FileManager extends Module {
             if (!($stream = fopen($options['file'], 'wb')))
                 $this->fail($options['file'].': Unable to open file for export stream');
 
-            foreach ($files as $m) {
-                $f = AttachmentFile::lookup($m->id);
+            foreach ($files as $f) {
                 if ($options['verbose'])
-                    $this->stderr->write($m->name."\n");
+                    $this->stderr->write($f->name."\n");
 
                 // TODO: Log %attachment and %ticket_attachment entries
                 $info = array('file' => $f->getInfo());
@@ -296,8 +316,11 @@ class FileManager extends Module {
                 }
                 // Create a new file
                 else {
-                    $fm = FileModel::create($finfo);
-                    if (!$fm->save() || !($f = AttachmentFile::lookup($fm->id))) {
+                    // Bypass the AttachmentFile::create() because we do not
+                    // have the data to send yet.
+                    $f = new AttachmentFile($finfo);
+                    $f->__new__ = true;
+                    if (!$f->save(true)) {
                         $this->fail(sprintf(
                             '%s: Unable to create new file record',
                             $finfo['name']));
@@ -374,10 +397,8 @@ class FileManager extends Module {
                     }
 
                     // Update file to record current backend
-                    $sql = 'UPDATE '.FILE_TABLE.' SET bk='
-                        .db_input($bk->getBkChar())
-                        .' WHERE id='.db_input($f->getId());
-                    if (!db_query($sql) || db_affected_rows()!=1)
+                    $f->bk = $bk->getBkChar();
+                    if (!$f->save())
                         return false;
 
                 } // end try
@@ -399,7 +420,7 @@ class FileManager extends Module {
 
         case 'zip':
             // Create a temporary ZIP file
-            $files = FileModel::objects();
+            $files = AttachmentFile::objects();
             $this->_applyCriteria($options, $files);
             if (!$options['file'])
                 $this->fail('Please specify zip file with `-f`');
@@ -409,20 +430,21 @@ class FileManager extends Module {
                     ZipArchive::CREATE)))
                 $this->fail($reason.': Unable to create zip file');
 
-            foreach ($files as $m) {
-                $f = AttachmentFile::lookup($m->id);
+            foreach ($files as $f) {
                 if ($options['verbose'])
-                    $this->stderr->write($m->name."\n");
-                $name = Format::encode(sprintf(
-                    '%d-%s', $f->getId(), $f->getName()
-                    ), 'utf-8', 'cp437');
+                    $this->stderr->write($f->name."\n");
+                $info = pathinfo($f->getName());
+                $name = Charset::transcode(
+                    sprintf('%s-%d.%s',
+                        $info['filename'], $f->getId(), $info['extension']),
+                    'utf-8', 'cp437');
                 $zip->addFromString($name, $f->getData());
             }
             $zip->close();
             break;
 
         case 'expunge':
-            $files = FileModel::objects();
+            $files = AttachmentFile::objects();
             $this->_applyCriteria($options, $files);
 
             foreach ($files as $m) {
@@ -445,7 +467,8 @@ class FileManager extends Module {
             if (!$val) continue;
             switch ($name) {
             case 'ticket':
-                $qs->filter(array('tickets__ticket_id'=>$val));
+                $qs->filter(array('attachments__thread_entry__thread__ticket__ticket_id'=>$val));
+                $qs->distinct('id');
                 break;
             case 'file-id':
                 $qs->filter(array('id'=>$val));
@@ -457,10 +480,11 @@ class FileManager extends Module {
                 $qs->filter(array('bk'=>$val));
                 break;
             case 'status':
-                if (!in_array($val, array('open','closed')))
+                if (!in_array($val, array('open','closed','archived','deleted')))
                     $this->fail($val.': Unknown ticket status');
 
-                $qs->filter(array('tickets__ticket__status'=>$val));
+                $qs->filter(array('attachments__thread_entry__thread__ticket__status__state'=>$val));
+                $qs->distinct('id');
                 break;
 
             case 'min-size':
@@ -488,40 +512,4 @@ class FileManager extends Module {
         }
     }
 }
-
-require_once INCLUDE_DIR . 'class.orm.php';
-
-class FileModel extends VerySimpleModel {
-    static $meta = array(
-        'table' => FILE_TABLE,
-        'pk' => 'id',
-        'joins' => array(
-            'tickets' => array(
-                'null' => true,
-                'constraint' => array('id' => 'TicketAttachmentModel.file_id')
-            ),
-        ),
-    );
-}
-class TicketAttachmentModel extends VerySimpleModel {
-    static $meta = array(
-        'table' => TICKET_ATTACHMENT_TABLE,
-        'pk' => 'attach_id',
-        'joins' => array(
-            'ticket' => array(
-                'null' => false,
-                'constraint' => array('ticket_id' => 'TicketModel.ticket_id'),
-            ),
-        ),
-    );
-}
-
-class AttachmentModel extends VerySimpleModel {
-    static $meta = array(
-        'table' => ATTACHMENT_TABLE,
-        'pk' => array('object_id', 'type', 'file_id'),
-    );
-}
-
 Module::register('file', 'FileManager');
-?>