Refactor and cleanup Cocoa code