Add repository factory and various fixes