Refactoring castle code structure