   
   // MARK: - Variables
   
-  /**
-   Name of NSManagedObject entity
-   */
+  /// Name of NSManagedObject entity
   static var entityName: String {
-    get {
-      return String(describing: self)
-    }
+    return String(describing: self)
   }
   
   // MARK: - Helpers
   
-  /**
-   Create new NSManagedObject in context
-   
-   - parameter inContext: context for object
-   */
-  static func new(inContext context: NSManagedObjectContext) -> NSManagedObject {
-    
-    return NSEntityDescription.insertNewObject(forEntityName: self.entityName, into: context)
-  }
-  
-  /**
-   Move NSManagedObject to another context
-   
-   - parameter context: context to move
-   */
-  func inContext(_ context: NSManagedObjectContext) -> NSManagedObject? {
-    
-    if self.objectID.isTemporaryID {
+  /// New NSManagedObject in context
+  ///
+  /// - Parameter context: context for object
+  /// - Returns: new object in context
+  static func new<T: NSManagedObject>(in context: NSManagedObjectContext) -> T? {
+    return NSEntityDescription.insertNewObject(forEntityName: entityName, into: context) as? T
+  }
+  
+  /// Move NSManagedObject to another context
+  ///
+  /// - Parameter context: context to move
+  /// - Returns: object in new context
+  func inContext<T: NSManagedObject>(_ context: NSManagedObjectContext) -> T? {
+    if objectID.isTemporaryID {
       do {
-        try self.managedObjectContext?.obtainPermanentIDs(for: [self])
+        try managedObjectContext?.obtainPermanentIDs(for: [self])
+        
       } catch {
         return nil
       }
     }
     
     do {
-      let objectInNewContext = try context.existingObject(with: objectID)
-      return objectInNewContext
+      return try context.existingObject(with: objectID) as? T
+      
     } catch {
       return nil
     }
   }
   
-  /**
-   Truncate all data from Entity in context
-   
-   - parameter inContext: context for search
-   */
+  /// Truncate all data from Entity in context
   static func deleteAllObjects() {
-    
-    // Initialize Fetch Request
-    let request: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: self.entityName)
-    
-    NSManagedObjectContext.saveWithBlock({
-      localContext in
-      
+    let request: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: entityName)
+    
+    NSManagedObjectContext.saveWithBlock({ localContext in
       do {
         let result = try localContext.fetch(request)
         

    // Try to perform fetch request
    do {
      try fetchedResultsController.performFetch()
    } catch {
      
    }
    
    // Return NSFetchedResultsController
    return fetchedResultsController as? NSFetchedResultsController<T>
  }
  
  // MARK: - fetchAll by attribute in context
  
  /**
   Fetch all objects by attribute in context
   
   - parameter byAttribute: name of attribute for search
   - parameter withInt64Value: value for search
   - parameter sortBy: column name for sorting
   - parameter ascending: sorting order
   - parameter inContext: context for search
   - parameter delegate: NSFetchedResultsControllerDelegate
   */
  @available(OSX 10.12, *)
  static func fetchAll<T>(byAttribute attribute: String, withInt64Value value: Int64, sortBy: String, ascending: Bool, inContext context: NSManagedObjectContext, delegate: NSFetchedResultsControllerDelegate) -> NSFetchedResultsController<T>? {
    
    // Build predicate with value
    let predicate = NSPredicate(format: "\(attribute) = \(value)")
    
    // Fetch and return data
    return self.fetchAll(withPredicate: predicate, sortBy: sortBy, ascending: ascending, inContext: context, delegate: delegate)
  }
  
  // MARK: - Aggregate functions
  
  /**
   Sum by attribute
   
   - parameter byAttribute: name of attribute for search
   - parameter withPredicate: predicate for search
   - parameter inContext: context for search
   */
  static func sum(onAttribute attribute: String, withPredicate predicate: NSPredicate, inContext context: NSManagedObjectContext) -> NSNumber? {
    
    // Initialize Fetch Request
    let request: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: self.entityName)
    
    // Add Predicate
    request.predicate = predicate
    
    // Set result type

    // Try to perform fetch request
    do {
      try fetchedResultsController.performFetch()
      return fetchedResultsController
      
    } catch {
      if CoreDuck.printErrors {
        print("⚠️ failed to fetch request for " + entityName)
      }
      return nil
    }
  }
  
  /// Fetch all objects with predicate in context
  ///
  /// - Parameters:
  ///   - predicate: NSPredicate for search
  ///   - sortedBy: column name to sort by
  ///   - ascending: direction to sort by
  ///   - delegate: NSFetchedResultsControllerDelegate
  ///   - context: NSManagedObjectContext to fetch request
  /// - Returns: NSFetchedResultsController
  @available(OSX 10.12, *)
  static func fetchAll<T: NSManagedObject>(with predicate: NSPredicate, sortedBy: String, ascending: Bool, delegate: NSFetchedResultsControllerDelegate, in context: NSManagedObjectContext = NSManagedObjectContext.mainContext) -> NSFetchedResultsController<T>? {
    let request: NSFetchRequest<T> = NSFetchRequest(entityName: entityName)
    
    // Sort Descriptors
    let sortDescriptor = NSSortDescriptor(key: sortedBy, ascending: ascending)
    request.sortDescriptors = [sortDescriptor]
    
    // Add Predicate
