added logic to prevent nil values breaking it