Refactoring to finally get type-safety