Refactored + matrix code included