Cleaned up and commented. Fixed some return code paths to match MD.framework. Removed dead code and fixed potential case for unbalanced mutex lock.