Refactoring data structures + fix crash