Moved entire js map code inside if block