Removed recursion and supporting max 3 levels