Merge code practically complete. needs verification and testing